package com.linsir.template.tag;

import com.linsir.lin.pojo.BaseCode;
import com.linsir.lin.service.BaseCodeService;
import freemarker.core.Environment;
import freemarker.ext.beans.BeansWrapper;
import freemarker.ext.beans.BeansWrapperBuilder;
import freemarker.template.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.Writer;
import java.util.List;
import java.util.Map;

/**
 * @author ：linsir
 * @date ：Created in 2021/7/5 17:09
 * @description：字典数据下拉别表标签
 * @modified By：
 * @version: 1.0.0
 */
@Component
public class DictSelectDirective implements TemplateDirectiveModel {

    /**
     * 对于@Component ，在spring中是无法注入Service的
     * 这里采用，静态注入的方式引入
     *
     */
    //@Resource
    private static BaseCodeService baseCodeService;

    @Autowired
    public void setBaseCodeService(BaseCodeService baseCodeService)
    {
        DictSelectDirective.baseCodeService=baseCodeService;
    }

    @Override
    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
        String typeCode=params.get("typeCode").toString();
        List<BaseCode> baseCodeList = baseCodeService.list(typeCode);
        BeansWrapper beansWrapper = new BeansWrapperBuilder(Configuration.VERSION_2_3_21).build();
        env.setVariable("baseCodeList",beansWrapper.wrap(baseCodeList));
        body.render(env.getOut());

    }
}
